package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.AlreadyChargeMonthAndItemStatQueryVO;
import com.ce.pms.report.vo.AlreadyChargeMonthAndItemStatReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：财务已收款分月分项统计表 service
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军
 * Created on 2008-10-29
 * Edit author:
 * Edit on:
 * 修改原因:
 */
public class AlreadyChargeMonthAndItemStatService  implements ICEReportDataSetService{

	private IReportDao reportDao ;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg0) throws CEReportException {

		AlreadyChargeMonthAndItemStatQueryVO queryVO = (AlreadyChargeMonthAndItemStatQueryVO) arg0;

		return new CEReportDataSet(getdata(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取数据  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<AlreadyChargeMonthAndItemStatReportVO> getdata(AlreadyChargeMonthAndItemStatQueryVO queryVO){
		String precinct = queryVO.getPrecinct();
		String subentry= queryVO.getSubentry();
		String beginDate = DateUtil.convertDateToStr(queryVO.getBeginDate(), "yyyyMM");
		String endDate = DateUtil.convertDateToStr(queryVO.getEndDate(), "yyyyMM");
		String buildId = "";
		if (BlankUtil.isBlank(queryVO.getBuildingId())){
			buildId = "%";
		}else{
			buildId = queryVO.getBuildingId().toString();
		}
		Object[] obj = new Object[]{subentry,subentry,subentry,
				subentry,subentry,subentry,subentry,subentry,
				subentry,beginDate,endDate,buildId,precinct};

		List<Object[]> temp = reportDao.getDataList("alreadyChargeMonthAndItemStat", obj);
		List<AlreadyChargeMonthAndItemStatReportVO> list = arrayToVo(temp);

		return list;
	}

	/**
	 * 功能描述:对象数组转 vo 数组  <br>
	 * @param temp
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<AlreadyChargeMonthAndItemStatReportVO> arrayToVo (List<Object[]> temp) {
		if(BlankUtil.isBlank(temp)){
			return null;
		}
		List<AlreadyChargeMonthAndItemStatReportVO> list = new ArrayList<AlreadyChargeMonthAndItemStatReportVO>();
		for (Object[] obj : temp) {
			AlreadyChargeMonthAndItemStatReportVO reportVO = new AlreadyChargeMonthAndItemStatReportVO();
			String period = (String)obj[1];
			String payMonth = "";
			if (!BlankUtil.isBlank(period)){
				String year = period.substring(0,4);
				String month = period.substring(4);
				payMonth = year+"年"+month+"月";
			}

			reportVO.setPayMonth(payMonth);		//收费月份
			reportVO.setActualPay(setValue(obj[2]));	//实收金额
			reportVO.setActualThis(setValue(obj[3]));	//实收本月
			reportVO.setActualBefore(setValue(obj[4]));//实收往月
			reportVO.setLatefee(setValue(obj[5]));	//滞纳金
			reportVO.setOverheadExpenses(setValue(obj[6]));//管理费
			reportVO.setOnticUpkeep(setValue(obj[7]));	//本体维修基金
			reportVO.setElectricityPay(setValue(obj[8]));//电费
			reportVO.setWaterPay(setValue(obj[9]));	//水费
			reportVO.setSewagePay(setValue(obj[10]));	//污水费
			reportVO.setGarbagePay(setValue(obj[11]));//垃圾处理费
			reportVO.setUpkeepPay(setValue(obj[12]));	//维修费
			reportVO.setAdjustSum(setValue(obj[13]));	//调整费用合计
			reportVO.setDerateSum(setValue(obj[14]));	//减免费用合计

			list.add(reportVO);
		}
		return list;
	}

	//	传入的值必须是 BigDecimal 类型的,否则返回空如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(AlreadyChargeMonthAndItemStatQueryVO queryVO){
		Map<String, Object> map =new HashMap<String, Object>();
		String beginDate = DateUtil.convertDateToStr(queryVO.getBeginDate(), "yyyy年MM月");
		String endDate = DateUtil.convertDateToStr(queryVO.getEndDate(), "yyyy年MM月");
		map.put("reportHeadStr", "收款月份："+beginDate+" 至 "+endDate);
		map.put("printDate", new Date());

		String subentry = "";
		if("1".equals(queryVO.getSubentry())){
			subentry="本月费用";
		}else if("2".equals(queryVO.getSubentry())){
			subentry="往月欠款";
		}else{
			subentry="本月费用 + 往月欠款";
		}
		map.put("subentry", subentry);

		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}

}
